#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <gtk/gtk.h>
#include<string.h>
#include "support.h"
#include "newinfo.h"
#include "message.h"
#include "mainwindow.h"
#include "clist.h"

struct newinfo_struct newinfo;

void on_button_new_info_help_clicked(GtkButton *button,gpointer user_data)
{
	GtkWidget *window_new_info=(GtkWidget *)user_data;
	gchar msg[]="在新建的信息中，姓名不能为空!";
	message_box_for_new_info(window_new_info,msg);
}


void on_button_new_info_cancel_clicked(GtkButton *button,gpointer  user_data)
{
	GtkWidget *window_new_info=(GtkWidget *)user_data;
	gtk_widget_destroy(window_new_info);
}


void on_button_new_info_ok_clicked(GtkButton *button,gpointer user_data)
{
	struct newinfo_struct *newinfo=(struct newinfo_struct *)user_data;
	struct file_ctrl *global_file=newinfo->global_file;
        gint i,j,len;
	gchar *info[MAX_COLUMNS];
	gchar info_nums[10];

	#ifdef _DEBUG_
		printf("on_button_new_info_ok_clicked:start---\n");
	#endif
	info[0]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_name));
	if(strlen(info[0])==0) {
		on_button_new_info_help_clicked(NULL,newinfo->window);
		return;
	}
	info[1]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_class));
	info[2]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_tel));
	info[3]=(const gchar *)gtk_entry_get_text(GTK_ENTRY(newinfo->entry_mail));
	if(strlen(info[3])!=0 && strcmp(info[3],"---")!=0 && strchr(info[3],'@')==NULL) {
		gchar msg[]="在新建的信息中，邮箱格式不正确!";
		message_box_for_new_info(newinfo->window,msg);
		return ;
	}
	info[4]=gtk_entry_get_text(GTK_ENTRY(newinfo->entry_blog));
	
	for(i=0;i<global_file->cur_file->col_nums;i++) {
		len=strlen(info[i]);
		for(j=0;j<len;j++) {
			if(info[i][j]==' ')
				info[i][j]='-';
		}
		if(strlen(info[i])==0) {
			info[i]="---";
		} 
	}
	gtk_clist_append(GTK_CLIST(newinfo->clist->clist_show),(gchar **)info);
	*newinfo->clist->show_all_row+=1;//increase current informations
	/*set modify flags with TRUE to mark current data file has been modified*/
	global_file->cur_file->modify=TRUE;
	global_file->data_mod=TRUE;
	save_tmp_file(newinfo);
	/*we select the new information*/
	gtk_clist_select_row(GTK_CLIST(newinfo->clist->clist_show),
							*newinfo->clist->show_all_row-1,0);
	g_snprintf(info_nums,10,"%d",global_file->cur_file->info_nums);
	/*
	 * we must selected the new information which you add so that you can
	 * see the new information obviously
	 */
	gtk_clist_set_text(GTK_CLIST(newinfo->clist->clist_group),
						   newinfo->clist->group_selected_row,
						   1,info_nums);
	#ifdef _DEBUG_
		printf("global_cur_file:info_nums=%d,%d\n",global_file->cur_file->info_nums,
							*newinfo->clist->show_all_row);
	#endif
	gtk_widget_destroy(newinfo->window);
}

void on_window_new_info_destroy(GtkObject *object,gpointer user_data)
{
	gtk_widget_destroy(GTK_WIDGET(object));
}
